﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.users
{
    public partial class user_edit : Web.UI.ManagePage
    {
        string defaultpassword = "0|0|0|0"; //默认显示密码
        protected string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
   
        //设置跳转参数
        protected string user_name = string.Empty;
        protected string user_avatar = string.Empty;
        protected string mobile = string.Empty;
        protected int user_id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
               
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.users().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    return;
                }
            }
            else
            {
                string _pwd = Utils.GetRamCode();
                txtPassword.Text = _pwd;
                txtPassword1.Text = _pwd;
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("user_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                string _str = "is_lock=0";
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    _str = "id>0";
                }
                //TreeBind("is_lock=0"); //绑定组别
                TreeBind(_str); //绑定组别
                GetProvince();
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                    ddlGroupId.Attributes.Add("disabled", "disabled");

                }
            }
        }

        #region 绑定组别=================================
        private void TreeBind(string strWhere)
        {
            BLL.user_groups bll = new BLL.user_groups();
            DataTable dt = bll.GetList(0, strWhere, "grade asc,id asc").Tables[0];

            this.ddlGroupId.Items.Clear();
            this.ddlGroupId.Items.Add(new ListItem("请选择组别...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                this.ddlGroupId.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
            }

           
        }
        #endregion

        #region 绑定省份=================================
        private void GetProvince()
        {
            BLL.province bll = new BLL.province();
            DataTable dt = bll.GetList(0, "", "id asc").Tables[0];

            this.ddlProvince.Items.Clear();
            this.ddlProvince.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["province_name"].ToString().Trim();

                this.ddlProvince.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定城市=================================
        private void GetCity(int province_id)
        {
            BLL.city bll = new BLL.city();
            DataTable dt = bll.GetList(0, "province_id=" + province_id, "id asc").Tables[0];

            this.ddlCity.Items.Clear();
            this.ddlCity.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["city_name"].ToString().Trim();

                this.ddlCity.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定城市=================================
        private void GetDistrict(int city_id)
        {
            BLL.district bll = new BLL.district();
            DataTable dt = bll.GetList(0, "city_id=" + city_id, "id asc").Tables[0];

            this.ddlDistrict.Items.Clear();
            this.ddlDistrict.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["district_name"].ToString().Trim();

                this.ddlDistrict.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
          
            BLL.users bll = new BLL.users();
            Model.users model = bll.GetModel(_id);

            ddlGroupId.SelectedValue = model.group_id.ToString();
            rblStatus.SelectedValue = model.status.ToString();
            txtUserName.Text = model.user_name;
            //txtUserName.ReadOnly = true;
            txtUserName.Attributes.Remove("ajaxurl");
            if (!string.IsNullOrEmpty(model.password))
            {
                txtPassword.Attributes["value"] = txtPassword1.Attributes["value"] = defaultpassword;
            }
            txtEmail.Text = model.email;
            txtNickName.Text = model.nick_name;
            txtAvatar.Text = model.avatar;
            rblSex.SelectedValue = model.sex;
            txtMobile.Text = model.mobile;
            txtMobile.Attributes.Remove("ajaxurl");
            txtQQ.Text = model.qq;
            txtAddress.Text = model.address;
            txtAmount.Text = model.amount.ToString();
            txtPoint.Text = model.point.ToString();
            lblRegTime.Text = model.reg_time.ToString();

            ddlProvince.SelectedValue = model.province_id.ToString();
            if (model.province_id > 0)
            {
                GetCity(model.province_id);
            }
            ddlCity.SelectedValue = model.city_id.ToString();
            if (model.city_id > 0)
            {
                GetDistrict(model.city_id);
            }
            ddlDistrict.SelectedValue = model.district_id.ToString();

            if (model.group_id > 1)
            {
                txtupgrade_time.Enabled = true;
                txtupgrade_time.Text = model.upgrade_time + "";
            }
            else { txtupgrade_time.Enabled = false; }
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.users model = new Model.users();
            BLL.users bll = new BLL.users();

            model.group_id = int.Parse(ddlGroupId.SelectedValue);
            model.status = int.Parse(rblStatus.SelectedValue);
            //检测用户名是否重复
            if (bll.Exists(txtUserName.Text.Trim()))
            {
                return false;
            }
            model.user_name = Utils.DropHTML(txtUserName.Text.Trim());
            //获得6位的salt加密字符串
            model.salt = Utils.GetCheckCode(6);
            //以随机生成的6位字符串做为密钥加密
            model.user_pwd = txtPassword.Text.Trim();
            model.email = Utils.DropHTML(txtEmail.Text);
            model.nick_name = Utils.DropHTML(txtNickName.Text);

            string avatar = Utils.DropHTML(txtAvatar.Text);
            if (!avatar.Contains("shicaiquanzi") && !string.IsNullOrEmpty(avatar))
            {
                avatar = "pic.shicaiquanzi.com" + avatar;
            }
            model.avatar = avatar;

            model.sex = rblSex.SelectedValue;
            model.mobile = Utils.DropHTML(txtMobile.Text.Trim());
            model.qq = Utils.DropHTML(txtQQ.Text);
            model.address = Utils.DropHTML(txtAddress.Text.Trim());
            model.amount = decimal.Parse(txtAmount.Text.Trim());
            model.point = int.Parse(txtPoint.Text.Trim());
            model.reg_time = DateTime.Now;

            model.province_id = Utils.StrToInt(ddlProvince.SelectedValue, 0);
            model.city_id = Utils.StrToInt(ddlCity.SelectedValue, 0);
            model.district_id = Utils.StrToInt(ddlDistrict.SelectedValue, 0);
            if (txtupgrade_time.Text + "" != "") { model.upgrade_time = DateTime.Parse(txtupgrade_time.Text); }

            int user_result = bll.Add(model);

            if (user_result > 0)
            {
                //AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加用户:" + model.user_name); //记录日志
                this.user_name = model.user_name;
                this.user_avatar = model.avatar;
                this.mobile = model.mobile;
                this.user_id = user_result;

                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.users bll = new BLL.users();
            Model.users model = bll.GetModel(_id);

            //model.group_id = int.Parse(ddlGroupId.SelectedValue);
            model.status = int.Parse(rblStatus.SelectedValue);
            model.user_name = Utils.DropHTML(txtUserName.Text.Trim());
            //判断密码是否更改
            if (txtPassword.Text.Trim() != defaultpassword)
            {
                //获取用户已生成的salt作为密钥加密
                model.salt = Utils.GetCheckCode(6);
                model.user_pwd = txtPassword.Text.Trim();
            }

            model.email = Utils.DropHTML(txtEmail.Text);
            model.nick_name = Utils.DropHTML(txtNickName.Text);

            string avatar = Utils.DropHTML(txtAvatar.Text);
            if (!avatar.Contains("shicaiquanzi") && !string.IsNullOrEmpty(avatar))
            {
                avatar = "pic.shicaiquanzi.com" + avatar;
            }
            model.avatar = avatar;

            model.sex = rblSex.SelectedValue;
            model.mobile = Utils.DropHTML(txtMobile.Text.Trim());
            model.qq = Utils.DropHTML(txtQQ.Text);
            model.address = Utils.DropHTML(txtAddress.Text.Trim());
            model.amount = Utils.StrToDecimal(txtAmount.Text.Trim(), 0);
            model.point = Utils.StrToInt(txtPoint.Text.Trim(), 0);

            model.province_id = Utils.StrToInt(ddlProvince.SelectedValue, 0);
            model.city_id = Utils.StrToInt(ddlCity.SelectedValue, 0);
            model.district_id = Utils.StrToInt(ddlDistrict.SelectedValue, 0);
            if (txtupgrade_time.Text + "" != "") { model.upgrade_time = DateTime.Parse(txtupgrade_time.Text); }
            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改用户信息:" + model.user_name); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("user_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改会员成功！", "user_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("user_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加会员成功！", "/admin/company/company_edit.aspx?user_name=" + this.user_name + "&mobile=" + this.mobile + "&avatar=" + this.user_avatar + "&user_id=" + this.user_id.ToString());
            }
        }
        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            int province_id = int.Parse(ddlProvince.SelectedValue);
            GetCity(province_id);
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            int city_id = int.Parse(ddlCity.SelectedValue);
            GetDistrict(city_id);
        }
    }
}